home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Text / WASTE / WASTE 1.1.2 Distribution / WASTE Source / WEDebug.p < prev    next >
Encoding:
Text File  |  1995-10-12  |  2.8 KB  |  99 lines  |  [TEXT/CWIE]

  1. unit WEDebug;
  2.  
  3. { WASTE PROJECT }
  4. { Debugging Routines }
  5.  
  6. { Copyright © 1993-1994 Marco Piovanelli }
  7. { All Rights Reserved }
  8.  
  9. interface
  10.     uses
  11.         WEInterface;
  12.  
  13. {$IFC WASTE_DEBUG}
  14.     procedure _WEAssert (condition: Boolean;
  15.                                     message: string);
  16.     procedure _WESanityCheck (hWE: WEHandle);
  17. {$ENDC}
  18.  
  19. implementation
  20.  
  21. {$IFC WASTE_DEBUG}
  22.  
  23.     procedure _WEAssert (condition: Boolean;
  24.                                     message: string);
  25.     begin
  26.         if (condition = false) then
  27.             begin
  28.                 message := Concat('Assertion Failed: ', message);
  29.                 DebugStr(message);
  30.             end;
  31.     end;  { _WEAssert }
  32.  
  33.     procedure _WESanityCheck (hWE: WEHandle);
  34.  
  35. { _WESanityCheck performs several checks on two key data structures: }
  36. { the run array and the style table, verifying a number of assertions. }
  37. { This routine made it possible to identify many subtle bugs during development. }
  38.  
  39.         var
  40.             pWE: WEPtr;
  41.             pRuns: RunArrayPtr;
  42.             pStyles: StyleTablePtr;
  43.             i, j, refCount: LongInt;
  44.     begin
  45.  
  46. { we aren't going to move memory }
  47.         pWE := hWE^;
  48.         pRuns := pWE^.hRuns^;
  49.         pStyles := pWE^.hStyles^;
  50.  
  51. { check the consistency of the run array }
  52. { first runStart must be zero }
  53.         _WEAssert(pRuns^[0].runStart = 0, 'First run array element is bad');
  54.  
  55. { last (dummy) runStart must be textLength + 1 and styleIndex must be -1 }
  56.         _WEAssert((pRuns^[pWE^.nRuns].runStart = pWE^.textLength + 1) and (pRuns^[pWE^.nRuns].styleIndex = -1), 'Last run array element is bad');
  57.  
  58. { all runs must be at least one character long }
  59.         for i := pWE^.nRuns - 1 downto 0 do
  60.             _WEAssert(pRuns^[i + 1].runStart - pRuns^[i].runStart > 0, 'Run length less than one');
  61.  
  62. { no two consecutive runs may reference the same style }
  63.         for i := pWE^.nRuns - 1 downto 0 do
  64.             _WEAssert(pRuns^[i + 1].styleIndex <> pRuns^[i].styleIndex, 'Spurious run boundary');
  65.  
  66. { all run array elements (except the last dummy entry) must reference an existing style }
  67.         j := pWE^.nStyles;
  68.         for i := pWE^.nRuns - 1 downto 0 do
  69.             _WEAssert((pRuns^[i].styleIndex >= 0) and (pRuns^[i].styleIndex < j), 'Invalid style index');
  70.  
  71. { the number of runs referencing each style in the style table }
  72. { must match the style reference count }
  73.         for j := pWE^.nStyles - 1 downto 0 do
  74.             begin
  75.                 refCount := 0;
  76.                 for i := pWE^.nRuns - 1 downto 0 do
  77.                     if (pRuns^[i].styleIndex = j) then
  78.                         refCount := refCount + 1;
  79.                 _WEAssert(pStyles^[j].refCount = refCount, 'Bad style reference count');
  80.             end;
  81.  
  82. { there may not be two identical entries in the style table (except for unused entries) }
  83.         for i := pWE^.nStyles - 1 downto 1 do
  84.             begin
  85.                 if (pStyles^[i].refCount = 0) then
  86.                     Cycle;
  87.                 for j := i - 1 downto 0 do
  88.                     begin
  89.                         if (pStyles^[j].refCount = 0) then
  90.                             Cycle;
  91.                         _WEAssert(_WEBlockCmp(@pStyles^[i].info, @pStyles^[j].info, SizeOf(pStyles^[i].info)) = false, 'Duplicate entry in style table');
  92.                     end;
  93.             end;
  94.  
  95.     end;  { _WESanityCheck }
  96.  
  97. {$ENDC}
  98.  
  99. end.